﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using PTTK19.BUS;

namespace PTTK19.GUI
{
    public partial class QuanLyLoaiSanPham : Form
    {
        public QuanLyLoaiSanPham()
        {
            InitializeComponent();
        }

        private void QuanLyLoaiSanPham_Load(object sender, EventArgs e)
        {
            HienThiDanhSachLoaiSanPham();
        }
        public void HienThiDanhSachLoaiSanPham()
        {
            dgvLoaiSanPham.DataSource = BUS_SanPham.LayDanhSachLoaiSP();
            btnDongY.Visible = false;
        }

        private void dgvLoaiSanPham_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int indexrow = e.RowIndex;
            DataGridViewRow row = dgvLoaiSanPham.Rows[indexrow];
            txtMaLoaiSP.Text = row.Cells[0].Value.ToString();
            txtTenLoaiSP.Text = row.Cells[1].Value.ToString();
        }

        private void btnThemLoaiSP_Click(object sender, EventArgs e)
        {
            btnDongY.Visible = true;
            btnThemLoaiSP.Visible = false;
            btnXoaLoaiSP.Enabled = false;
            btnCapNhatLoaiSP.Enabled = false;
        }

        private void btnXoaLoaiSP_Click(object sender, EventArgs e)
        {
            BUS_SanPham sp = new BUS_SanPham();
            int MaLoaiSP= int.Parse(txtMaLoaiSP.Text);
            int iKQ = 0;
            iKQ = BUS_SanPham.XoaLoaiSanPham(MaLoaiSP);
            if (iKQ > 0)
            {
                MessageBox.Show("Xóa sản phẩm thành công.");
                dgvLoaiSanPham.DataSource = null;
                HienThiDanhSachLoaiSanPham();
            }
            else
            {
                MessageBox.Show("Xóa sản phẩm thất bại.");
            }
        }

        private void btnCapNhatLoaiSP_Click(object sender, EventArgs e)
        {
            BUS_SanPham sp = new BUS_SanPham();
            sp.TenLoaiSP = txtTenLoaiSP.Text;
            sp.MaLoaiSP = int.Parse(txtMaLoaiSP.Text);
            int iKQ = 0;
            iKQ = BUS_SanPham.CapNhatLoaiSanPham(sp);
            if (iKQ > 0)
            {
                MessageBox.Show("Cập nhật sản phẩm thành công.");
                dgvLoaiSanPham.DataSource = null;
                HienThiDanhSachLoaiSanPham();
            }
            else
            {
                MessageBox.Show("Cập nhật phẩm thất bại.");
            }
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (txtTenLoaiSP.Text == "")
            {
                MessageBox.Show("Hãy nhập tên sản phẩm.");
            }
            else
            {
                BUS_SanPham sp = new BUS_SanPham();
                sp.TenLoaiSP = txtTenLoaiSP.Text;
                int iKQ = 0;
                iKQ = BUS_SanPham.ThemLoaiSanPham(sp);
                if (iKQ > 0)
                {
                    MessageBox.Show("Thêm sản phẩm thành công.");
                    dgvLoaiSanPham.DataSource = null;
                    HienThiDanhSachLoaiSanPham();
                }
                else
                {
                    MessageBox.Show("Thêm sản phẩm thất bại.");
                }
            }
            btnDongY.Visible = false;
            btnThemLoaiSP.Visible = true;
            btnXoaLoaiSP.Enabled = true;
            btnCapNhatLoaiSP.Enabled = true;
        }
    }
}
